// 实现这个项目的构建任务

const {src, dest, series, parallel, watch} = require('gulp')

const del = require('del')
const loadPlugins = require('gulp-load-plugins')
const bs = require('browser-sync')

const plugins = loadPlugins()

const data = {
    menus: [{
            name: 'Home',
            icon: 'aperture',
            link: 'index.html'
        },
        {
            name: 'Features',
            link: 'features.html'
        },
        {
            name: 'About',
            link: 'about.html'
        },
        {
            name: 'Contact',
            link: '#',
            children: [{
                    name: 'Twitter',
                    link: 'https://twitter.com/w_zce'
                },
                {
                    name: 'About',
                    link: 'https://weibo.com/zceme'
                },
                {
                    name: 'divider'
                },
                {
                    name: 'About',
                    link: 'https://github.com/zce'
                }
            ]
        }
    ],
    pkg: require('./package.json'),
    date: new Date()
}

// 默认资源路径
const opts = {
    dist: 'dist',
    dev: 'dev',
    src: 'src',
    publish: 'publish',
    styles: 'src/assets/styles/**.scss',
    scripts: 'src/assets/scripts/**.js',
    pages: 'src/**.html',
    images: 'src/assets/images/**',
    fonts: 'src/assets/fonts/**',
    extras: 'public/**'
}

// 清理构建资源
const clean = () => {
    return del([opts.dist, opts.dev])
}

// 样式文件
const style = () => {
    return src(opts.styles, {base: opts.src})
        .pipe(plugins.sass({
            outputStyle: 'expanded'
        }))
        .pipe(dest(opts.dev))
}

// 脚本文件
const script = () => {
    return src(opts.scripts, {base: opts.src})
        .pipe(plugins.babel({
            presets: ['@babel/preset-env']
        }))
        .pipe(dest(opts.dev))
}

// 页面文件
const page = () => {
    return src(opts.pages, {base: opts.base})
        .pipe(plugins.swig({
            data: data,
            defaults: { cache: false }
        }))
        .pipe(dest(opts.dev))
}

// 媒体文件
const image = () => {
    return src(opts.images, {base: opts.src})
        .pipe(plugins.imagemin())
        .pipe(dest(opts.dist))
}

// 字体文件
const font = () => {
    return src(opts.fonts, {base: opts.src})
        .pipe(plugins.imagemin())
        .pipe(dest(opts.dist))
}

// 其它资源
const extra = () => {
    return src(opts.extras, {base: opts.publish})
        .pipe(dest(opts.dist))
}

// web服务
const serve = () => {
    watch(opts.styles, style)
    watch(opts.scripts, script)
    watch(opts.pages, page)

    watch([
        opts.images,
        opts.fonts,
        opts.extras
    ], bs.reload)

    bs.init({
        notify: false,
        port: 2020,
        files: 'dev/**',
        server: {
            baseDir: ['dev', 'src', 'public'],
            routes: {
                '/node_modules': 'node_modules'
            }
        }
    })
}

// 构建注释(useref)
const useref = () => {
    return src(opts.dev + '/**.html', {base: opts.dev})
        .pipe(plugins.useref({
            searchPath: [ opts.dev, '.']
        }))
        .pipe(plugins.if(/\.js$/, plugins.uglify()))
        .pipe(plugins.if(/\.css$/, plugins.cleanCss()))
        .pipe(plugins.if(/\.html$/, plugins.htmlmin({
            collapseWhitespace: true,
            minifyCSS: true,
            minifyJS: true
        })))
        .pipe(dest(opts.dist))
}

// 任务流
const compile = parallel(style, script, page)
const build = series(
    clean, 
    parallel(
        series(compile, useref), 
        image, 
        font, 
        extra
    )
)
const develop = series(compile, serve)

// 输出的任务
module.exports = {
    clean,
    build,
    compile,
    develop
}